<?php
session_start();
require_once '../utils/Response.php';
require_once '../utils/ResponseByPage.php';
require_once '../config/dbConfig.php';
global $conn;
connectDB();

/**
 * 分页获取心理健康数据
 * @param int $currentPage 当前页码
 * @param int $pageSize 每页数量
 * @return void
 */
function getMentalsByPage($currentPage = 1, $pageSize = 10)
{
    global $conn;
    
    // 计算偏移量
    $offset = ($currentPage - 1) * $pageSize;
    
    // 获取总记录数
    $countSql = "SELECT COUNT(*) as total FROM mental_content";
    $countResult = $conn->query($countSql);
    $totalCount = $countResult->fetch_assoc()['total'];
    
    // 获取分页数据
    $sql = "SELECT mc.*, mt.name as type_name 
            FROM mental_content mc 
            LEFT JOIN mental_type mt ON mc.type_id = mt.id 
            ORDER BY mc.create_time DESC 
            LIMIT ? OFFSET ?";
            
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ii", $pageSize, $offset);
    $stmt->execute();
    $result = $stmt->get_result();
    $data = $result->fetch_all(MYSQLI_ASSOC);
    
    ResponseByPage::success('获取成功', $totalCount, $currentPage, $pageSize, $data)->send();
    
    $stmt->close();
    $conn->close();
}

/**
 * 搜索心理健康数据并分页
 * @param string $searchStr 搜索关键词
 * @param int $currentPage 当前页码
 * @param int $pageSize 每页数量
 * @return void
 */
function searchMentalsByPage($searchStr, $currentPage = 1, $pageSize = 10)
{
    global $conn;
    
    $searchTerm = "%$searchStr%";
    
    // 获取搜索结果总数
    $countSql = "SELECT COUNT(*) as total FROM mental_content WHERE title LIKE ? OR content LIKE ?";
    $countStmt = $conn->prepare($countSql);
    $countStmt->bind_param("ss", $searchTerm, $searchTerm);
    $countStmt->execute();
    $totalCount = $countStmt->get_result()->fetch_assoc()['total'];
    
    // 计算偏移量
    $offset = ($currentPage - 1) * $pageSize;
    
    // 获取搜索分页数据
    $sql = "SELECT mc.*, mt.name as type_name 
            FROM mental_content mc 
            LEFT JOIN mental_type mt ON mc.type_id = mt.id 
            WHERE mc.title LIKE ? OR mc.content LIKE ? 
            ORDER BY mc.create_time DESC 
            LIMIT ? OFFSET ?";
            
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssii", $searchTerm, $searchTerm, $pageSize, $offset);
    $stmt->execute();
    $result = $stmt->get_result();
    $data = $result->fetch_all(MYSQLI_ASSOC);
    
    ResponseByPage::success('搜索成功', $totalCount, $currentPage, $pageSize, $data)->send();
    
    $stmt->close();
    $countStmt->close();
    $conn->close();
}

/**
 * 查询心理健康类型数据
 * @return void
 */
function getMentalsType()
{
    global $conn;
    
    $sql = "SELECT * FROM mental_type ORDER BY id ASC";
    $result = $conn->query($sql);
    $data = $result->fetch_all(MYSQLI_ASSOC);
    
    Response::success('获取类型成功', $data)->send();
    
    $conn->close();
}

/**
 * 根据类型ID获取心理健康数据
 * @param int $typeId 类型ID
 * @param int $currentPage 当前页码
 * @param int $pageSize 每页数量
 * @return void
 */
function getMentalsByType($typeId, $currentPage = 1, $pageSize = 10)
{
    global $conn;
    
    // 计算偏移量
    $offset = ($currentPage - 1) * $pageSize;
    
    // 获取总记录数
    $countSql = "SELECT COUNT(*) as total FROM mental_content WHERE type_id = ?";
    $countStmt = $conn->prepare($countSql);
    $countStmt->bind_param("i", $typeId);
    $countStmt->execute();
    $totalCount = $countStmt->get_result()->fetch_assoc()['total'];
    
    // 获取分页数据
    $sql = "SELECT mc.*, mt.name as type_name 
            FROM mental_content mc 
            LEFT JOIN mental_type mt ON mc.type_id = mt.id 
            WHERE mc.type_id = ? 
            ORDER BY mc.create_time DESC 
            LIMIT ? OFFSET ?";
            
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("iii", $typeId, $pageSize, $offset);
    $stmt->execute();
    $result = $stmt->get_result();
    $data = $result->fetch_all(MYSQLI_ASSOC);
    
    ResponseByPage::success('获取成功', $totalCount, $currentPage, $pageSize, $data)->send();
    
    $stmt->close();
    $countStmt->close();
    $conn->close();
}


/**
 * 根据ID获取心理健康数据
 * @param int $id 数据ID
 * @return void
 */
function getMentalById($id){
    global $conn;
    $sql = "SELECT mc.*, u.username 
            FROM mental_content mc
            LEFT JOIN user u ON mc.create_user = u.id 
            WHERE mc.id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    $data = $result->fetch_assoc();
    Response::success('获取成功', $data)->send();
    $stmt->close();
    $conn->close();
}


/**
 * 通过用户id查找心理健康文章数据
 * @param $userId
 * @param int $currentPage 当前页码
 * @param int $pageSize 每页数量
 * @return void
 */
function getMentalByUserId($userId, $currentPage = 1, $pageSize = 10){
    global $conn;
    $offset = ($currentPage - 1) * $pageSize;

    $countSql = "SELECT COUNT(*) as total FROM mental_content WHERE create_user = ?";
    $countStmt = $conn->prepare($countSql);
    $countStmt->bind_param("i", $userId);
    $countStmt->execute();
    $totalCount = $countStmt->get_result()->fetch_assoc()['total'];
    
    $sql = "SELECT mc.*, mt.name as type_name 
            FROM mental_content mc 
            LEFT JOIN mental_type mt ON mc.type_id = mt.id 
            WHERE mc.create_user = ? 
            ORDER BY mc.create_time DESC 
            LIMIT ? OFFSET ?";
            
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("iii", $userId, $pageSize, $offset);
    $stmt->execute();
    $result = $stmt->get_result();
    $data = $result->fetch_all(MYSQLI_ASSOC);
    
    ResponseByPage::success('获取成功', $totalCount, $currentPage, $pageSize, $data)->send();
    
    $stmt->close();
    $countStmt->close();
    $conn->close();
}



